package cn.com.surker.util;


import cn.hutool.core.io.resource.ClassPathResource;

import java.io.IOException;
import java.util.Properties;

/**
 * @Author lucky
 * @create 2021年03月25 9:32
 * @desc 获取配置文件帮助类
 */
public class PropertiesUtils {

    public static void main(String[] args){
        System.out.println(getAttribute("config/application-dev.properties", "corpID"));
        System.out.println(getAttribute("config/application-dev.properties", "corpSecret"));
        System.out.println(getAttribute("config/application-dev.properties", "agentId"));
        System.out.println(getAttribute("config/application-dev.properties", "webUrl"));
    }

    /**
     * 获取属性值
     * @param fileName 配置文件名
     * @param attributeName 属性名
     * @return
     */
    public static Object getAttribute(String fileName, String attributeName) {
        Properties properties = loadProperties(fileName);
        if (StringUtils.isNull(properties))
            return null;

        return properties.get(attributeName);
    }

    /**
     * 获取配置文件
     * @param fileName 文件名称
     * @return
     */
    public static Properties loadProperties(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        Properties properties = new Properties();
        try {
            properties.load(classPathResource.getStream());
            return properties;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
